<template>
  <div class="main">
    <div class="login">
      <div>111</div>
      <div class="box"></div>
      <p style="text-align: center">登录页面</p>
      <div>
        <input type="text" v-model="form.user" placeholder="请输入用户名" />
        <input type="password" v-model="form.psd" placeholder="请输入密码" />
      </div>
      <div class="btns">
        <button @click="getLogin">登录</button>
      </div>
    </div>
  </div>
</template>
<script>
import { getLogin } from "@/api/index.js";
import { Message } from "element-ui";
export default {
  data() {
    return {
      redirect: "",
      form: {
        user: "admin",
        psd: "123456",
      },
    };
  },
  //动态路由
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    getLogin() {
      // 登录
      getLogin(this.form).then((res) => {
        console.log(res, "res");
        if (res.code === 200) {
          sessionStorage.setItem("userInfo", JSON.stringify(res.data));
          // this.$store.dispatch("getMenuList", res.data.userInfo);
          this.$router.push({ path: this.redirect || "/" });
          Message.success({
            message: res.msg,
          });
        } else {
          Message.error({
            message: res.msg,
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.box {
  margin: 40px;
}
.main {
  height: 100vh;
  position: relative;
  .login {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-250px, -150px);
    width: 500px;
    height: 300px;
    border-radius: 10px;
    background-color: skyblue;
    .btns {
      display: flex;
      flex-direction: column;
      align-items: center;
      button {
        height: 50px;
        width: 100px;
        margin-top: 50px;
      }
    }
  }
}
</style>
